package net.optix.ip.vo
{
	import mx.collections.ArrayCollection;
	
	public class FirmInventionVO
	{	
		public var firmName:String;
		public var firmID:int;
		public var avCostPerPa:Number	=	0;
		public var avCostPerAb:Number	=	0;
		public var avOARPerPa:Number	=	0;
		public var avOARPerAb:Number	=	0;
		
		public var sumFillingCostPa:Number	=	0;
		public var sumFillingCostAb:Number	=	0;
		
		public var sumResponseCostPa:Number	=	0;
		public var sumResponseCostAb:Number	=	0;

		public var numInventionAb:int;
		public var numInventionIss:int;
		
			
		public var avRCEsPerPa:Number;
		public var avRCEsPerAb:Number;
		public var avAppPerPa:Number;
		
		public var avAppealsPerAbandonment:Number;
		
		
			
		public static function purseFirst(tracks:ArrayCollection):ArrayCollection
		{
			var firms:ArrayCollection	=	new ArrayCollection();
			
			var firmID:int	=	0;
			var arr:ArrayCollection	;
			for(var i:int=0; i<tracks.length; i++)
			{
				var track:TrackVO	=	TrackVO(tracks[i]);
				
				if(track.firmID != firmID)
				{
					if(i==0)
					{
						arr	=	new ArrayCollection();
						arr.addItem(track);
					}
					else
					{
						//trace("purse sdfasdf    "+i);
						firms.addItem(FirmInventionVO.purseSecond(arr));
						arr	=	new ArrayCollection();
						arr.addItem(track);
					}
					
				}
				else
				{
					
					arr.addItem(track);
				}
				firmID	=	track.firmID;
			}
			firms.addItem(FirmInventionVO.purseSecond(arr));
			var over:FirmInventionVO	=	new FirmInventionVO();
			over.firmName				=	"Overall";
			for(var j:int=0; j<firms.length; j++)
			{
				var firm:FirmInventionVO	=	FirmInventionVO(firms[j]);
				over.numInventionAb			+=	firm.numInventionAb;
				over.numInventionIss		+=	firm.numInventionIss;
				
				over.sumFillingCostPa			+=	firm.sumFillingCostPa;
				over.sumFillingCostAb			+=	firm.sumFillingCostAb;
				
				over.sumResponseCostPa		+=	firm.sumResponseCostPa;
				over.sumResponseCostAb		+=	firm.sumResponseCostAb;
			}
			FirmInventionVO.calculate(over);
			firms.addItem(over);
			for(i; i<firms.length; i++)
			{
				trace(firms[i].firmName+"  firmname");
			}
			return firms;
		}
		
		public static function purseSecond(tracks:ArrayCollection):FirmInventionVO
		{	
			trace(tracks+"   tracks");
			var firm:FirmInventionVO	=	new FirmInventionVO();
			
			var track:TrackVO;
			track	=	TrackVO(tracks[0]);
			firm.firmID	=	track.firmID;
			firm.firmName=	track.firmName;
				
			var inventionID:int;
			var arr:ArrayCollection	=	new ArrayCollection();
			for(var i:int=0; i<tracks.length; i++)
			{
				track	=	TrackVO(tracks[i]);
				
				if(track.inventionID != inventionID)
				{
					if(i==0)
					{
						arr	=	new ArrayCollection();
						arr.addItem(track);
					}
					else
					{
						FirmInventionVO.purseThird(firm,arr);
						arr	=	new ArrayCollection();
						arr.addItem(track);
					}
					
				}
				else
				{
					
					arr.addItem(track);
				}
				inventionID	=	track.inventionID;
			}
			FirmInventionVO.purseThird(firm,arr);
			FirmInventionVO.calculate(firm);
			return firm;
		}
		
		public static function purseThird(firm:FirmInventionVO,tracks:ArrayCollection):void
		{
			var track:TrackVO;
			
			track	=	TrackVO(tracks[0]);
			
			if(track.inventionType	== 9)
			{
				firm.numInventionIss	+=	1;
			}
			else if(track.inventionType == 10)
			{
				firm.numInventionAb	+=	1;
			}
			
			for(var i:int=0; i<tracks.length; i++)
			{
				track	=	TrackVO(tracks[i]);
				
				switch(track.actionType)
				{
					case 2:
						if(track.inventionType == 9)
						{
							firm.sumFillingCostPa	+=	track.actionCost;	
						}
						else if(track.inventionType == 10)
						{
							firm.sumFillingCostAb	+=	track.actionCost;
						}
						break;
					case 3:
						if(track.inventionType == 9)
						{
							firm.sumResponseCostPa	+=	track.actionCost;	
						}
						else if(track.inventionType == 10)
						{
							firm.sumResponseCostAb	+=	track.actionCost;
						}
						break;
					case 5:
						if(track.inventionType == 9)
						{
							firm.sumFillingCostPa	+=	track.actionCost;	
						}
						else if(track.inventionType == 10)
						{
							firm.sumFillingCostAb	+=	track.actionCost;
						}
						break;
				}
			}	
		}
		
		public static function calculate(track:FirmInventionVO):void
		{
			try
			{
				if(track.numInventionIss > 0)
				{
					track.avCostPerPa	=	track.sumFillingCostPa/track.numInventionIss;
					track.avOARPerPa	=	track.sumResponseCostPa/track.numInventionIss;
				}
				if(track.numInventionAb > 0)
				{
					track.avCostPerAb	=	track.sumFillingCostAb/track.numInventionAb;
					track.avOARPerAb	=	track.sumResponseCostAb/track.numInventionAb;
				}
			}
			catch(error:Error){}
		}
	}
	
}